$(document).ready(function(){
    $("#search_term").keyup(function(){
        doSearch();
    });
    $("#search_type").change(function(){
        doSearch();
    });
    
    doSearch();
});

function doSearch(){
    $("#search_results").html("");
        
    var search_term = $("#search_term").val();
    var search_type = $("#search_type").val();

    $.post(getBaseURL()+"settings/manage_users/searchResults/", 
        {
            query: search_term,
            filter: search_type
        },
        function(data){
            $("#user_list").html(data);
        }
    ); 
}

function registerButtons(){
    $("#delete_user").click(function(e){
        var $this = $(this);
        e.preventDefault();
        if (confirm("Are you sure you wish to delete this user?")){
            $.post($("#delete_user").attr("href"), 
                {user_id: $this.attr('value')},
                function(data){
                    location.reload();
                }
            );
        }
    });
}

function selectUser(elem, id){
    $('#user_list .listItem').removeClass('selected');
    $(elem).addClass('selected');
    $.post(getBaseURL()+"settings/manage_users/previewUser/", 
        {user_id: ""+id+""},
        function(data){
            $("#preview_user").html(data);
            registerButtons();
        }
    );
}